package com.atguigu.order.controller;

import com.atguigu.commonutils.result.R;
import com.atguigu.order.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author kabuda
 * @create 2023-05-19 9:53
 */
@RestController
@RequestMapping("/eduorder/paylog")
public class PayLogController {
    @Autowired
    private PayLogService payService;

    /**
     * 生成支付二维码
     * @param orderNo
     * @return
     */
    @PostMapping("/createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {
        Map map = payService.createNative(orderNo);
        System.out.println("生成二维码："+map);
        return R.ok().data(map);
    }

    /**
     * 查询支付状态做相应措施
     * @param orderNo
     * @return
     */
    @PostMapping("/queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo) {
        Map map = payService.queryPayStatus(orderNo);
        System.out.println("查询状态："+map);
        if(map==null){
            return R.error().message("支付失败");
        }
        if(map.get("trade_state").equals("SUCCESS")){
            //支付成功更改订单状态、生成支付日志
            this.payService.updateOrdersStatus(map);
            return R.ok();
        }
        return R.ok().code(25000).message("支付中");
    }
}
